<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Stream
 *
 * @author QuangKhoi
 */
class Stream {
    //put your code here
    /**
     *
     * @var Int
     */
    public $streamId;
    /**
     *
     * @var Int 
     */
    public $userId;
    /**
     *
     * @var String 
     */
    public $name;
    /**
     *
     * @var Int
     */
    public $type;
    /**
     *
     * @var String
     */
    public $query;
    /**
     *
     * @var Int 
     */
    public $language;
    /**
     *
     * @var Int
     */
    public $location;
    /**
     *
     * @var Int
     */
    public $platform;
    /**
     *
     * @var String
     */
    public $externalSources;
    /**
     *
     * @var Int
     */
    public $folderId;
    
    /**
     *
     * @var Date
     */
    public $dateCreated;
    
    /**
     * Default constructor
     */
    public function __construct() {
        
    }
    
    /**
     * Read folder infos from the database
     * @param Int $id
     */
    public static function withId($id) {
        $instance = new self();
        $db = Database::getInstance();
        $id = base64_decode($id);
        $result = $db->query("SELECT * FROM streams WHERE streamid = '$id'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->streamId = $rows["streamid"];
                $instance->userId = $rows["userid"];
                $instance->name = $rows["name"];
                $instance->type = $rows["type"];
                $instance->query = $rows["query"];
                $instance->language = $rows["language"];
                $instance->location = $rows["location"];
                $instance->platform = $rows["platform"];
                $instance->externalSources = $rows["externalsources"];
                $instance->folderId = $rows["folderid"];
                $instance->dateCreated = $rows["datecreated"];
                    return $instance;
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView(), 
                    "404", "Not found any stream with provided id");
                $controller->returnError();
                return NULL;
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }
    
    /**
     * Create new Folder object to store
     * @param Int $userid
     * @param String $folderName
     * @param Int $parentId
     */
    public static function withData($userid, $name, $type, $query, $language, 
            $location, $platform, $externalSources, $folderId) {
        $instance = new self();
        $instance->userId = $userid;
        $instance->name = $name;
        $instance->type = $type;
        $instance->query = $query;
        $instance->language = $language;
        $instance->location = $location;
        $instance->platform = $platform;
        $instance->externalSources = $externalSources;
        $instance->folderId = $folderId;
        return $instance;
    }
    
    public static function listStream($folderid) {
        $db = Database::getInstance();
        $userId = $_SESSION["userid"];
        if($folderid == 0) {
            $query = "SELECT * FROM streams WHERE userid='$userId'";
        }
        else{
            $query = "SELECT * FROM streams WHERE folderid = '$folderid' AND userid='$userId'";
        }
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                foreach ($result->dataTable->rows as $row) {
                    $id = $row['streamid'];
                    $name = $row['name'];
                    $folder = $row['folderid'];
                    $streamId = "$streamId$id;";
                    $streamname = "$streamname$name;";
                    $folderId = "$folderId$folder;";
                }
                return array (
                    'streamid' => $streamId,
                    'streamname' => $streamname,
                    'folderid' => $folderId
                );
            } else {
                //$controller = new ErrorController(new Error(), new ErrorView(), "404", "Not found any user with provided email");
                //$controller->returnError();
                return array(
                    'streamid' => 'undefined',
                    'streamname' => 'undefined',
                    'folderid' => 'undefined'
                );
            }
        } else {
            //$controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            //$controller->returnError();
            return array(
                'streamid' => 'undefined',
                'streamname' => 'undefined',
                'folderid' => 'undefined'
            );
        }
    }
    
     public static function deleteStream($streamid) {
        $db = Database::getInstance();
        $query = "DELETE FROM streams WHERE streamid = '$streamid'"; 
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            return "true";
        }
        else {
            /*$controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();*/
            return "false";
        }
     }
     
    public static function copyStream($streamid, $folderid) {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM streams WHERE streamid = '$streamid'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $streamId = $db->getID("streams", "streamid");
                $userId = $rows["userid"];
                $name = $rows["name"];
                $type = $rows["type"];
                $cquery = $rows["query"];
                $language = $rows["language"];
                $location = $rows["location"];
                $platform = $rows["platform"];
                $externalSources = $rows["externalsources"];
                $folderId = $folderid;
                $dateCreated = $rows["datecreated"];
                if($folderId != $rows["folderid"]) {
                    $query = "INSERT INTO streams (streamid, userid, name, type, query, language, 
                        location, platform, externalsources, folderid, datecreated) VALUES 
                        ('$streamId', '$userId', '$name', '$type', '$cquery', '$language', '$location', 
                        '$platform', '$externalSources', '$folderId', '$dateCreated')";
                    $result = $db->query($query);
                    if ($result->returnCode == 200) {
                        return 'true';
                    }
                    else {
                        /*$controller = new ErrorController(new Error(), new ErrorView, 
                                $result->returnCode, $result->message);
                        $controller->returnError();*/
                        return 'false';
                    }
                }
            }
            else {
                return 'false';
            }
        }
        else {
            return 'false';
        }
     }
}

?>
